package com.a3dweijing.wetouch2.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.ImageLoaderUtil;
import com.a3dweijing.wetouch2.adapter.SpecialListBottomRecAdapter2;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.ExpoonVideo;
import com.a3dweijing.wetouch2.bean.Special;
import com.a3dweijing.wetouch2.bean.SpecialExpoonMsg;
import com.a3dweijing.wetouch2.callback.OnClick;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.google.gson.Gson;

/**
 * Created by Yxd on 2017/11/14.
 */

public class SpecialBriefActivity extends BaseActivity implements OnClick {
    private RecyclerView specialRec;
    private SpecialListBottomRecAdapter2 adapter;
    private Handler handler;
    private Special special;
    private ImageView pic;
    private TextView name;
    private TextView content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_specialbrief);
        special = (Special) getIntent().getSerializableExtra("special");
        Log.e("special", special.toString());
        specialRec = (RecyclerView) findViewById(R.id.special_brief_rec);
        pic = (ImageView) findViewById(R.id.special_brief_title_img);
        ImageLoaderUtil.getLoader().loadImg4(special.getSpecial_topic_img(), pic);
        name = (TextView) findViewById(R.id.special_brief_title_name);
        name.setText(special.getSpecialtopic_headline());
        content = (TextView) findViewById(R.id.special_brief_title_content);
        content.setText(special.getSpecial_topic_describe());
        specialRec.setLayoutManager(new LinearLayoutManager(getBaseContext()));
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 100:
                        SpecialExpoonMsg specialExpoonMsg = new Gson().fromJson(msg.obj.toString(), SpecialExpoonMsg.class);
                        if (specialExpoonMsg != null && specialExpoonMsg.getVideoList() != null && specialExpoonMsg.getVideoList().size() > 0) {
                            adapter = new SpecialListBottomRecAdapter2(specialExpoonMsg.getVideoList());
                            adapter.setOnClick(SpecialBriefActivity.this);
                            specialRec.setAdapter(adapter);
                        }
                        break;
                    case 404:
                        Toast.makeText(getBaseContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        };

        if (special != null) {
            Log.e("--", special.getSpecial_topic_id() + "-");
            new GetSpecialThread(special.getSpecial_topic_id() + "", 1 + "").start();
        }
    }

    @Override
    public void onclick(int position, Object o) {
        Intent intent = new Intent(SpecialBriefActivity.this,
                ExpoonWebViewActivity.class);
        intent.putExtra("url",((ExpoonVideo)o).getVideoUrl());
        startActivity(intent);
    }

    private class GetSpecialThread extends Thread {
        private String specialid;
        private String pageNo;

        public GetSpecialThread(String specialid, String pageNo) {
            this.specialid = specialid;
            this.pageNo = pageNo;
        }

        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetSpecialList(specialid, pageNo);
            Log.e("--", resulet);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(100, resulet));
            } else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }


    }
}
